package com.mycompany.integration;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

/**
 * Clase con Test de Integracion para testear el buen funcionamiento de operaciones GET del Web Service.
 * Es necesario que la aplicacion este desplegada en el siguiente contexto:
 * WADL: http://localhost:8080/ACLREST/services/api?_wadl
 * @author javachavez
 */
public class NetClientGetIT {
	protected final Log log = LogFactory.getLog(getClass());
	@Test
    public void testReadAll() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/readAll";
			
			URL url = new URL(strUrl );
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}
			
			conn.disconnect();
	}
	
	@Test
    public void testRead() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/read/1";
			
			URL url = new URL(strUrl );
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}
			
			conn.disconnect();
	}
}